/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vanniktech.onabilityresult.sample.slice;

import com.vanniktech.onabilityresult.sample.ForResultAbility;
import com.vanniktech.onabilityresult.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;

import java.util.Locale;

/**
 * 结果页面实例
 *
 * @since 2021-05-07
 */
public class ForResultAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private ForResultAbility mAbility = null;

    /**
     * onStart
     *
     * @param intent 意图
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_for_result);
        Button btnClose1 = (Button) findComponentById(ResourceTable.Id_btn_close1);
        Button btnClose2 = (Button) findComponentById(ResourceTable.Id_btn_close2);
        Button btnClose3 = (Button) findComponentById(ResourceTable.Id_btn_close3);

        btnClose1.setText(getString(ResourceTable.String_resultBtnText1).toUpperCase(Locale.ROOT));
        btnClose2.setText(getString(ResourceTable.String_resultBtnText2).toUpperCase(Locale.ROOT));
        btnClose3.setText(getString(ResourceTable.String_resultBtnText3).toUpperCase(Locale.ROOT));

        btnClose1.setClickedListener(this);
        btnClose2.setClickedListener(this);
        btnClose3.setClickedListener(this);

        mAbility = (ForResultAbility) getAbility();
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     *
     * @param intent 意图
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 点击事件
     *
     * @param component 组件
     */
    @Override
    public void onClick(Component component) {
        if (mAbility == null) {
            return;
        }
        switch (component.getId()) {
            case ResourceTable.Id_btn_close1:
                mAbility.setResult(MainAbilitySlice.RESULT_OK, null);
                break;
            case ResourceTable.Id_btn_close2:
                mAbility.setResult(MainAbilitySlice.RESULT_CANCELED, null);
                break;
            case ResourceTable.Id_btn_close3:
                mAbility.setResult(MainAbilitySlice.RESULT_USER_DEFINED, null);
                break;
            default:
                break;
        }
        terminateAbility();
    }
}
